﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WebCrawler
{
    class Program
    {
        static readonly HttpClient httpClient = new HttpClient();

        static async Task Main(string[] args)
        {
            Console.WriteLine("请输入关键字：");
            string keyword = Console.ReadLine();

            List<string> urls = await Search(keyword);
            Console.WriteLine($"共找到{urls.Count}个网页");

            HashSet<string> phoneNumbers = new HashSet<string>();
            foreach (string url in urls)
            {
                Console.WriteLine($"正在爬取{url}");
                string html = await httpClient.GetStringAsync(url);
                string pattern = @"((\d{3,4}-)?\d{7,8})";
                Regex regex = new Regex(pattern);
                MatchCollection matches = regex.Matches(html);

                foreach (Match match in matches)
                {
                    phoneNumbers.Add(match.Value);
                    Console.WriteLine($"找到电话号码{match.Value}");
                }
            }

            Console.WriteLine($"共找到{phoneNumbers.Count}个电话号码");
            Console.WriteLine("每个电话号码所属的URL：");
            foreach (string phoneNumber in phoneNumbers)
            {
                Console.WriteLine(phoneNumber);
                foreach (string url in urls)
                {
                    if (await ContainsPhoneNumber(url, phoneNumber))
                    {
                        Console.WriteLine($"  {url}");
                    }
                }
            }

            Console.ReadLine();
        }

        static async Task<List<string>> Search(string keyword)
        {
            string bingUrl = $"https://www.bing.com/search?q={keyword}";
            string html = await httpClient.GetStringAsync(bingUrl);

            string pattern = @"<a href=""(.*?)""";
            Regex regex = new Regex(pattern);
            MatchCollection matches = regex.Matches(html);

            List<string> urls = new List<string>();
            foreach (Match match in matches)
            {
                string url = match.Groups[1].Value;
                if (url.StartsWith("http") && !url.Contains("bing.com"))
                {
                    urls.Add(url);
                }
            }

            return urls;
        }

        static async Task<bool> ContainsPhoneNumber(string url, string phoneNumber)
        {
            string html = await httpClient.GetStringAsync(url);
            return html.Contains(phoneNumber);
        }
    }
}
